package pit.com.ordermobilesystem.fragment;

import java.util.ArrayList;

import pit.com.ordermobilesystem.BaseFragment;
import pit.com.ordermobilesystem.R;
import pit.com.ordermobilesystem.adapter.BaseTabAdapter;
import pit.com.ordermobilesystem.widget.CustomProgressDialog;
import pit.com.ordermobilesystem.widget.TabViewPaper;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;

public class HomeFragment extends BaseFragment{
	    CustomProgressDialog progressDialog;
	    Fragment mFrag;
	    private FragmentTabHost mTabHost;
	    ArrayList<BaseTabFragment> list=new ArrayList<BaseTabFragment>();
	    BaseTabAdapter mAdapter;
	    TabViewPaper mPager;
	    PagerSlidingTabStrip mIndicator;
	    int currentTab=0;
	    
	    @Override
	    public View onCreateView(LayoutInflater inflater, ViewGroup container,
	            Bundle savedInstanceState) {
	        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
		TabCategoryFragment categoryTab=new TabCategoryFragment();
		categoryTab.setTitleTab(getString(R.string.category));
		
		TabStoreFragment storeTab=new TabStoreFragment();
		storeTab.setTitleTab(getString(R.string.restaurant));
		
		
		TabNewsFragment newsTab=new TabNewsFragment();
		newsTab.setTitleTab(getString(R.string.news));
		
	        list.add(categoryTab);
	        list.add(storeTab);
	        list.add(newsTab);
	        
	        mAdapter = new BaseTabAdapter(getChildFragmentManager(),list);		        
	        mPager = (TabViewPaper)rootView.findViewById(R.id.pager);
	        //mPager.setDisableSwipe(true);
	        mPager.setOffscreenPageLimit(3);
	        mPager.setAdapter(mAdapter);
	        mIndicator = (PagerSlidingTabStrip) rootView.findViewById(R.id.indicator);
	        
	        mIndicator.setViewPager(mPager);       

	        mIndicator.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int index) {
				currentTab=index;
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	      
	        return rootView;
	    }
	    public boolean popFragment() {
			if(currentTab!=0)    mPager.setCurrentItem(0); 
		    boolean isPopFragment = false;
		    BaseTabFragment currentTabTag = list.get(currentTab);
		    isPopFragment = currentTabTag.popFragment();
		    return isPopFragment;
	    }	    
	    
	    public void addFragment(BaseFragment mFrag){
		    BaseTabFragment currentTabTag = list.get(currentTab);
		    currentTabTag.addFragment(mFrag);
	    }
	@Override
	public void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		list.get(currentTab).onActivityResult(requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}
	    
	    
	    
}
